package client;

import java.io.*;
import java.net.Socket;

/**
 * @author: liuchong
 * @create: 2023/2/20 19:53
 * @description:
 */
public class QuotationClient {
    public void client(String filePath, String host, int port) {
        try {
            //读取文件
            FileInputStream fis = new FileInputStream(filePath);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));
            while (true) {
                // 与服务端建立连接
                Socket socket = new Socket(host, port);
                // 获得输出流,给服务端发送信息
                OutputStream outputStream = socket.getOutputStream();
//                String str = "数据传输------";
                String temp;
                while ((temp = bufferedReader.readLine()) != null) {
                    outputStream.write(temp.getBytes());


                }
                // 通过shutdownOutput高速服务器已经发送完数据，后续只能接受数据
                socket.shutdownOutput();
                //                 接收服务端发送的消息
                InputStream inputStream = socket.getInputStream();
                byte[] outPut = new byte[4096];
                while (inputStream.read(outPut) > 0) {
                    String result = new String(outPut);
                    System.out.println("服务端反回的的消息是：" + result);
                }
                inputStream.close();
                outputStream.close();
                socket.close();
                Thread.sleep(3000);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
